<template>
	<view class="container" style="padding-bottom: 220rpx;">
		<itemBaseInfo :itemModel='itemModel'></itemBaseInfo>
		<view @click="payClick" style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;background-color: #FFF;margin-top: 10rpx;height: 88rpx;padding: 0 20rpx;">
			<view style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;">
				<view style="font-size: 30rpx;">选择</view>
				<view style="margin-left: 40rpx;color: #999;font-size: 26rpx;">规格</view>
			</view>
			<u-icon name="arrow-right" color="#999" size="15"></u-icon>
		</view>
		<itemDetail :itemModel='itemModel'></itemDetail>
		<!-- 规格选择弹窗 -->
		<itemspecPopup ref="itemspecPopup" :itemModel='itemModel' @skuSpecClick='skuSpecClick' @confirm='skuSpecConfirm' @close='itemspecPopupClose' @open='itemspecPopupOpen'></itemspecPopup>
		
		<view class="safeArea" style="position: fixed;bottom: 0;width: 100%;height: 140rpx;background: #FFFFFF;display: flex;flex-direction: row;align-items: center;justify-content:space-between;">
			<view style="display: flex;flex-direction: column;align-items: center;margin-left: 20rpx;" @click="goStockCart">
				<image style="width: 60rpx;height: 60rpx;" src="@/pageA/static/merchant/stockCart.png" mode="aspectFill"></image>
				<text style="font-size: 24rpx;">进货车</text>
			</view>
			<!-- 支付按钮 -->
			<view style="text-align: center;" @click="payClick"
			 :style="{
			      'width': '220rpx',
			      'height': '88rpx',
			      'line-height': '88rpx',
			      'color': '#FFF',
			      'background': '#FC5E53',
			      'font-size': '30rpx',
			      'font-weight': 'bold',
			      'margin-right': '20rpx',
			    }">加入进货车</view>
		</view>
	</view>
</template>

<script>
	import { merchant } from '@/api/merchant.js'
	import itemBaseInfo from '@/pageA/businessCenter/productLibrary/components/itemBaseInfo.vue'
	import itemDetail from '@/pageA/businessCenter/productLibrary/components/itemDetail.vue'
	import itemspecPopup from '@/pageA/businessCenter/productLibrary/components/itemspecPopup/itemspecPopup.vue'
	import {stockCart} from '@/api/stockCart.js'
	export default{
		components: {itemBaseInfo, itemDetail, itemspecPopup},
		data() {
			return {
				itemId: '',
				itemModel: {}
			}
		},
		onLoad() {
			this.itemId = this.$Route.query.itemId
			this.getItemInfo()
		},
		methods:{
			//获取item信息
			getItemInfo() {
				merchant.getItemInfo({itemId: this.itemId}).then(res => {
					if(res.data.code == 10000){
						var itemModel = res.data.data
						this.itemModel = itemModel
					}
				})
			},
			// 加入采购订单
			payClick(){
				this.findSpecList(this.itemModel.skuList)
				// 默认点击第一个
				this.itemModel.specList.forEach((item, index)=>{
					this.skuSpecClick('', index, 0)
				})
				this.$refs.itemspecPopup.show()
			},
			goStockCart(){
				this.$Router.push({
					name: 'stockCart'
				})
			},
			goCreateOrder(){
				stockCart.joinStockCart({
					item: this.itemModel.id,
					sku: this.itemModel.skuId,
					payCount: this.itemModel.payCount
				}).then(res => {
					if(res.data.code == 10000){
						this.$show.showMessage('加入成功')
						this.$refs.itemspecPopup.close()
					}
				})
			},
			
			// 弹窗立即支付点击事件
			skuSpecConfirm(e){
				for(var spec of this.itemModel.specList){
					var flag = 0
					this.itemModel.specList_sel = this.itemModel.specList_sel?this.itemModel.specList_sel:[]
					for(var spec_sel of this.itemModel.specList_sel){
						if(spec.specName == spec_sel.specName){
							flag = 1
						}
					}
					if(flag==0){
						this.$show.showMessage('请选择'+spec.specName)
						return
					}
				}
				this.itemModel.payCount = e
				this.goCreateOrder()
				this.$refs.itemspecPopup.close()
			},
			skuSpecClick(spec, index, index2){
				this.setSpecStatus(spec, index, index2)
				this.getSpecList_sel()
				this.getSpecs_sel()
				this.getSkuModel()
				this.$forceUpdate()
			},
			itemspecPopupClose(){
				
			},
			itemspecPopupOpen(){
				
			},
			setSpecStatus(spec, index, index2){
				//先把当前的组的所有select设false
				var specList = this.itemModel.specList
				var specValueList = specList[index].specValueList
				for (var i in specValueList) {
					var item = specValueList[i]
					if (index2 == i) {
						item.select = true
					} else {
						item.select = false
					}
				}
			},
			getSpecList_sel(){
				var specList = this.itemModel.specList
				//组装specList
				var specList_sel = []
				for (var item2 of specList) {
					for (var spec2 of item2.specValueList) {
						if (spec2.select) {
							var value = {}
							value.specName = spec2.specName
							value.name = spec2.name
							specList_sel.push(value)
						}
					}
				}
				this.itemModel.specList_sel = specList_sel
			},
			getSpecs_sel() {
				//将选中的spec以,隔开
				var specs_sel = ''
				for(var item of this.itemModel.specList_sel){
					specs_sel = specs_sel + item.name + '，'
				}
				specs_sel = specs_sel.substring(0, specs_sel.length-1)
				this.itemModel.specs_sel = specs_sel
			},
			//查找选中的sku
			getSkuModel(){
				var specValues = JSON.stringify(this.itemModel.specList_sel)
				for(var sku of this.itemModel.skuList){
					if(sku.specValues == specValues){
						if(sku.cover){
							this.itemModel.coverUrl = sku.coverUrl
						}
						this.itemModel.price = sku.costPrice
						this.itemModel.costPrice = sku.costPrice
						this.itemModel.stock = sku.stock
						this.itemModel.skuId = sku.id
					}
				}
			},
			//获取sku列表
			findSpecList(skuList) {
				//组装规格名称
				var valueList = []
				var specNames = new Set()
				for(var item of skuList){
					for(var value of JSON.parse(item.specValues)){
						specNames.add(value.specName)
						valueList.push(value)
					}
				}
				//组装规格
				var specList = []
				var specs_sel = ''
				for(var specName of specNames){
					var sku = {}
					var specSet = new Set()
					for(var item of valueList){
						if(specName == item.specName){
							specSet.add(item.name)
						}
					}
					sku.specName = specName
					//组装规格值
					var specListTemp = Array.from(specSet)
					var specValueList = []
					for(var i in specListTemp){
						var name = specListTemp[i]
						var spec = {}
						spec.specName = specName
						spec.name = name
						spec.select = false
						//设置默认选中的规格
						// for(var sel of JSON.parse(this.itemModel.defaultSku.specValues)){
						// 	if(sel.name == name){
						// 		spec.select = true
						// 		specs_sel = specs_sel + name + '，'
						// 	}
						// }
						specValueList.push(spec)
					}
					sku.specValueList = specValueList
					specList.push(sku)
				}
				this.$set(this.itemModel, 'specList', specList)
				// this.itemModel.specList = specList
				console.log('计算specList', this.itemModel)
				specs_sel = specs_sel.substring(0, specs_sel.length-1)
			},
		}
	}
</script>

<style scoped lang="scss">
	
</style>
